//
//  YHObject.h
//  YHObject
//
//  Created by Guanglu on 18/3/2019.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSObject (SHNSObject)
+ (nullable instancetype)objectFromJson:(id)json;
+ (nullable instancetype)objectFromDict:(NSDictionary *)dictionary;

- (nullable id)jsonObject;
- (nullable NSData *)jsonData;
- (nullable NSString *)jsonString;

- (void)sh_encodeWithCoder:(NSCoder *)coder;
- (instancetype)sh_initWithCoder:(NSCoder *)aDecoder;

@end

#pragma mark -
@interface SHObject : NSObject <NSCoding>
@end

#pragma mark -
@interface NSArray (SHNSObject)

/**
 Creates and returns an array from a json-array.
 This method is thread-safe.

 @param cls  The instance's class in array.
 @param json  A json array of `NSArray`, `NSString` or `NSData`.
 Example: [{"name","Mary"},{name:"Joe"}]

 @return A array, or nil if an error occurs.
 */
+ (nullable NSArray *)modelArrayWithClass:(Class)cls json:(id)json;

@end


/**
 Provide some data-model method for NSDictionary.
 */
@interface NSDictionary (SHNSObject)

/**
 Creates and returns a dictionary from a json.
 This method is thread-safe.

 @param cls  The value instance's class in dictionary.
 @param json  A json dictionary of `NSDictionary`, `NSString` or `NSData`.
 Example: {"user1":{"name","Mary"}, "user2": {name:"Joe"}}

 @return A dictionary, or nil if an error occurs.
 */
+ (nullable NSDictionary *)modelDictionaryWithClass:(Class)cls json:(id)json;
@end

NS_ASSUME_NONNULL_END
